package com.fighYo.trace.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fighYo.trace.entity.Info;
import com.fighYo.trace.entity.User;
import com.fighYo.trace.service.InfoService;
import com.fighYo.trace.service.UserService;
import com.fighYo.trace.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fighYo
 * @since 2022-04-04
 */
@CrossOrigin
@Slf4j
@Api(tags = {"用户个人信息管理"})
@RestController
@RequestMapping("/info")
public class InfoController {

    @Autowired
    private InfoService infoService;

    @Autowired
    private UserService userService;

    @ApiOperation("根据token获取用户个人信息")
    @GetMapping("get")
    public R getInfo(HttpServletRequest request) {
        String userId = request.getHeader("access-token");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id", userId);
        Info info = infoService.getOne(wrapper);
        return R.ok().data("item", info);
    }

    @ApiOperation("修改用户个人信息")
    @PostMapping("update")
    public R updateInfo(@RequestBody Info info) {
        Boolean result = infoService.updateById(info);
        User user = userService.getById(info.getUserId());
        user.setJudgeFlag("0");
        userService.updateById(user);
        return result ? R.ok() : R.error().msg("修改失败");
    }

}

